{% extends 'base.html' %}
{% load static %}
{% block title %}
    活动管理
{% endblock %}
{% block header_name %}
    活动管理
{% endblock %}
{% block bread %}
      <!-- Breadcrumb-->
    <div class="breadcrumb-holder container-fluid">
    <ul class="breadcrumb">
      <li class="breadcrumb-item"><a href="{% url 'index' %}">首页</a></li>
      <li class="breadcrumb-item active">活动管理</li>
    </ul>
    </div>
{% endblock %}
{% block body %}


    <section class="tables">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-12">
            <div class="modal fade" id="activityDeleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalScrollableTitle" aria-hidden="true">
              <div class="modal-dialog modal-dialog-scrollable" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalScrollableTitle">温馨提示</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                    确认删除该活动吗？
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-danger" id="delete-confirm-btn">确认删除</button>
                  </div>
                </div>
              </div>
            </div>
            <div class="card">
              <div class="card-header">
                <h1 style="display: inline-block">共{{ count }}个活动</h1>
                <a href="{% url 'activities:activity_create' %}" class="btn btn-primary pull-right">发布活动</a>
              </div>
              <div class="card-body">
                <div class="modal fade" id="showReason" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                  <div class="modal-dialog" role="document">
                    <div class="modal-content">
                      <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">不通过的原因</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="modal-body">
                        <div id="reason"></div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="table-responsive">
                  <table class="table table-striped table-hover table-bordered">
                    <thead>
                      <tr>
                        <th>活动名称</th>
                        <th>发布时间</th>
                        <th>主办方</th>
                        <th>类别</th>
                        <th>参与者学时</th>
                        <th>工作人员学时</th>
                        <th>观众学时</th>
                        <th>审核者</th>
                        <th>上级审核者</th>
                        <th>状态</th>
                        <th>操作</th>
                      </tr>
                    </thead>
                    <tbody>
                    {% with role=request.user.role %}
                      {% for activity in activities %}
                          <tr data-uid="{{ activity.uid }}">
                          <td style="font-weight: bold"><a href="{% url 'activities:activity_detail' activity.pk %}">{{ activity.name }}</a></td>
                          <td>{{ activity.updated_at }}</td>
                          <td>{{ activity.sponsor }}</td>
                          <td>{{ activity.get_credit_type_display }}</td>

                          <td style="color: blue">{{ activity.score_player }}</td>
                          <td style="color: blue">{{ activity.score_staff }}</td>
                          <td style="color: blue">{{ activity.score_viewer }}</td>
                          <td>{{ activity.to.name }}</td>
                          <td>{{ activity.to_school.name }}</td>
                          <td>
                              {% if activity.is_verifying %}
                                <i class="fa fa-spinner" style="color: gray"></i>
                              {% else %}
                                  {% if activity.is_verify %}
                                  <i class="fa fa-check" style="color: green"></i>
                                  {% else %}
                                    <i class="fa fa-remove" style="color: red"></i>
                                  {% endif %}
                              {% endif %}
                          </td>
                          <td>
                            {% if activity.is_verifying %}
                                <a href="{% url 'activities:activity_update' activity.pk %}" class="btn btn-warning btn-sm">编辑</a>
                                <a href="#" class="btn btn-danger btn-sm delete-btn" data-uid="{{ activity.uid }}" data-target="#activityDeleteModal" data-toggle="modal">删除</a>
                            {% else %}
                              {% if activity.is_verify %}
                                <a href="{% url 'activities:activity_detail' activity.pk %}" class="btn btn-primary btn-sm">查看详情</a>
                                {% else %}
                                <a href="#" class="btn btn-warning btn-sm showReasonBtn" data-toggle="modal" data-target="#showReason" data-reason="{{ activity.reason }}">查看原因</a>
                              {% endif %}
                            {% endif %}

                          </td>
                          </tr>
                      {% endfor %}
                    {% endwith %}
                    </tbody>
                  </table>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>

    {% include 'pagination.html' %}
{% endblock %}


{% block js %}
  <script>
    $(function () {
        var reason = "";
        var activity_id = "";
        $('.showReasonBtn').click(function () {
            reason = $(this).attr("data-reason");
            $('#reason').text(reason)
        });

        $('.delete-btn').click(function () {
            activity_id = $(this).attr("data-uid");
        });

        $('#delete-confirm-btn').click(function () {
            $.ajax({
                url: '/activities/delete/',
                cache: false,
                data: {
                    activity_id
                },
                method: 'POST',
                success: res => {
                    if (res.status === 'ok') {
                        window.location.reload()
                    } else {
                        alert("删除失败，请稍候重试！")
                    }
                }
            })
        })
    })

  </script>
{% endblock %}


